<template>
    <div style="width: 100%">
        <div id="deawer">
            <v-tableTitle :btnConfig="btnConfig" @cancel="cancel" @importFj="importFj" @exportFj="exportFj"></v-tableTitle>
            <div class="detailBox">
                <v-detail ref="detail"></v-detail>
            </div>
            <div class="detailListBox" v-if="!newFlag">
                <v-detailList ref="detailList"></v-detailList>
            </div>
        </div>
    </div>

</template>

<script>
    import baseMixin from "@/mixins/BaseMixin";
    import skipMixin from "@/mixins/SkipMixin";
    import config from "../Config"
    import tableTitle from "commons/TableTitle";
    import detail from "../details/Detail"
    import detailList from "../detailsList/DetailList"
    import bus from "../../../../../bus/bus";

    export default {
        name: "",
        mixins: [baseMixin, skipMixin, config],
        data() {
            return {
                newFlag: false,
                btnConfig: [
                    {label: this.$t('common.return'), invalid: "cancel"},
                    {label: this.$t('out.importFj'), invalid: "importFj"},
                    {label: this.$t('out.exportFj'), invalid: "exportFj"},
                ],
            };
        },

        created() {
        },
        methods: {
            //弹框取消
            cancel() {
                this.$router.push({name: "ymxprint"});
            },
            importFj() {
                this.$store.commit("setNewFlag", true);
                this.$router.push({name: 'importDeawer'});
            },
            async exportFj () {
                let res = await this.$api.get('/getYmxPrintFjList/' + this.$store.state.routeData.orderkey);
                
                    console.log(res.data.list);
                    res.data.list.forEach(async (item, index) => {
                        try {
                            console.log(item.ordersappendixkey);
                            console.log(item.ordersappendixname);
                            let param = {};
                            param.ordersappendixkey = item.ordersappendixkey;
                            param.ordersappendixname = item.ordersappendixname;
                            let res1 = await this.$api.post('/downYmxPrintFj',param, { responseType: 'blob' })
                            if (window.navigator.msSaveBlob) {
                                var blobObject = new Blob([res1.data]);
                                window.navigator.msSaveBlob(blobObject, item.ordersappendixname);
                            } else {
                                var blob = new Blob([res1.data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8' });
                                var filename = item.ordersappendixname;
                                var downloadElement = document.createElement('a');
                                var href = window.URL.createObjectURL(blob); //创建下载的链接
                                downloadElement.style.display = 'none';
                                downloadElement.href = href;
                                downloadElement.download = filename; //下载后文件名
                                document.body.appendChild(downloadElement);
                                downloadElement.click(); //点击下载
                                document.body.removeChild(downloadElement); //下载完成移除元素
                                window.URL.revokeObjectURL(href); //释放掉blob对象
                            }
                            this.$refs.table.delSelectData(item, index);
                        } catch (e) {
                            this.$message.error(e);
                        }
                    });
                
            },
        },
        mounted() {
            if (Object.keys(this.$store.state.skip).length !== 0) {
                let data = this.$store.state.skip;
                this.newFlag = data.newFlag;
            } else {
                this.newFlag = this.$store.state.newFlag;
            }

        },
        watch: {},
        components: {
            "v-tableTitle": tableTitle,
            "v-detail": detail,
            "v-detailList": detailList,
        }
    }
</script>

<style scoped>

</style>
